package com.bascker.algorithm.practice.dp;

import java.util.Objects;

/**
 * 买卖股票的最佳时机 II
 *  VS. BuyStock: 本次没有交易次数限制
 *
 * <p>
 * in: [7,1,5,3,6,4]       out: 7
 *   day2 买入, day3 卖出, profit = 4
 *   day4 买入, day5 卖出, profit = 3
 *   total = 7
 *
 *
 * @author bascker
 * @apiNote from leetcode T122
 */
public class BuyStockII {

    public int maxProfit(int[] prices) {
        if (Objects.isNull(prices) || prices.length == 0) {
            return 0;
        }

        int n = prices.length;
        int[][] dp = new int[n][2];

        dp[0][0] = 0;
        dp[0][1] = -prices[0];

        for (int i = 1; i < n; i ++) {
            dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
            dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
        }

        return dp[n - 1][0];
    }

}
